package com.android.phoenix.bluetooth;

import java.util.ArrayList;
import java.util.List;

import com.android.phoenix.R;
import com.baidu.navi.location.aa;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class BluDeviceListAdapter extends BaseAdapter{
	private Context context;
	private Handler mHandler;
	private static List<BluetoothDevice> listBluDevice;

	public BluDeviceListAdapter(Context paramContext, Handler handler) {
		this.context = paramContext;
		listBluDevice = new ArrayList<BluetoothDevice>(); 
		mHandler = handler;
	}
    public void add(BluetoothDevice device)
    {
    	for(BluetoothDevice mBluetoothDevice : listBluDevice)
    	{
    		if(mBluetoothDevice.getAddress().equals(device.getAddress()))
    		return;
    	}    	
    	listBluDevice.add(device);
    	notifyDataSetChanged();
    }
	@Override
	public int getCount() {
		return listBluDevice.size();
	}

	@Override
	public Object getItem(int position) {
		return listBluDevice.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) 
		{		
			convertView = LayoutInflater.from(context).inflate(
					R.layout.list_item_blu_device, null);
			holder = new ViewHolder();
			holder.textDeviceName = (TextView) convertView.findViewById(R.id.textDeviceName);
			holder.textDeviceMAC = (TextView) convertView
					.findViewById(R.id.textDeviceMAC);
			holder.imgDevice = (ImageView) convertView
					.findViewById(R.id.imgDevice);
			holder.textDeviceName.setText(listBluDevice.get(position).getName());
			holder.textDeviceMAC.setText(listBluDevice.get(position).getAddress());			
			holder.textDeviceName.setOnClickListener(new OnItemChildClickListener(position));
			holder.textDeviceMAC.setOnClickListener(new OnItemChildClickListener(position));
			holder.imgDevice.setOnClickListener(new OnItemChildClickListener(position));
			convertView.setTag(holder);
		} else {
			// Get the ViewHolder back to get fast access to the TextView
			// and the ImageView.
			holder = (ViewHolder) convertView.getTag();
			holder.textDeviceName.setText(listBluDevice.get(position).getName());
			holder.textDeviceMAC.setText(listBluDevice.get(position).getAddress());	
			
		}	
		// Bind the data efficiently with the holder.
		return convertView;
	}
		static class ViewHolder {
		private TextView textDeviceName;
		private TextView textDeviceMAC;
		private ImageView imgDevice;
	}
		private class OnItemChildClickListener implements View.OnClickListener {
			// 点击类型索引，对应前面的
			private int clickIndex;
			// 点击列表位置
			private int position;
			
			public OnItemChildClickListener(int position) {
				
				this.position = position;
			}

			@Override
			public void onClick(View v) {
				// 创建Message并填充数据，通过mHandle联系Activity接收处理
				Message msg = new Message();
				msg.what = position;
				Bundle b = new Bundle();
				b.putParcelable("Device", listBluDevice.get(position));
				msg.setData(b);
				mHandler.sendMessage(msg);
			}
			
		}

}
